<script setup>
import {
  reactive,
  ref,
  onMounted,
  computed,
  defineProps,
  defineExpose
} from 'vue';
const visibleDrawer = ref(false);
const open = (title) => {
  visibleDrawer.value = true;
};
const list = ref([
  { id: 1, value: 1, label: '科技' },
  { id: 2, value: 2, label: '宗教' },
  { id: 3, value: 3, label: '历史' }
]);
defineProps({
  title: {
    type: String
  }
});
defineExpose({
  open
});
const drawerForm = ref({
  title: '',
  card_name: '',
  card_ImgURL: '',
  content: ''
});
const rules = ref({
  title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  card_name: [
    {
      required: true,
      messsage: '请输入分类',
      trigger: 'blur'
    },
    {
      pattern: /^[a-zA-Z1-9]{3,12}$/,
      message: '请输入正确的类名'
    }
  ],
  card_ImgURL: [
    {
      required: true,
      messsage: '请输入分类',
      trigger: 'blur'
    }
  ],
  content: [
    {
      required: true,
      messsage: '请输入分类',
      trigger: 'blur'
    }
  ]
});
const imageUrl = ref('');

const handleAvatarSuccess = (response, uploadFile) => {
  imageUrl.value = URL.createObjectURL;
};

const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!');
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!');
    return false;
  }
  return true;
};
</script>

<template>
  <el-drawer
    size="35%"
    v-model="visibleDrawer"
    :title="title"
    :direction="direction"
    :before-close="handleClose"
  >
    <el-form :model="drawerForm" :rules="rules">
      <el-form-item label="文章分类" prop="title">
        <el-input placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="card_name">
        <Gselect :list="list"></Gselect>
      </el-form-item>
      <el-form-item label="文章封面" prop="card_ImgURL">
        <el-upload
          class="avatar-uploader"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="文章内容" prop="content"> </el-form-item>
      <el-form-item></el-form-item>
    </el-form>
  </el-drawer>
</template>

<style lang="scss" scoped></style>
